﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 图书管理.Model;
using 图书管理.Services;

namespace 图书管理
{
    public partial class BookInfoForm : Form
    {

        BookTypeUtils bookTypeUtils;
        BookInfoUtils bookInfoUtils;
        int selId;
        BookInfo bookInfo;

        public BookInfoForm()
        {
            InitializeComponent();

            bookTypeUtils = new BookTypeUtils();
            bookInfoUtils = new BookInfoUtils();

            bookInfo = new BookInfo();
        }

        private void BookInfoForm_Load(object sender, EventArgs e)
        {
            // 获取所有的类别
            IEnumerable<Model.BookTyple> bookTypes = bookTypeUtils.SelectAll();
            IEnumerator<Model.BookTyple> enumerator = bookTypes.GetEnumerator();
            List<string> types = new List<string>();
            while (enumerator.MoveNext())
            {
                Model.BookTyple current = enumerator.Current;
                types.Add(current.Name.Trim());
            }
            uiComboBox1.DataSource = types;

            RefreshList();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            bool v = bookInfoUtils.InsertBookInfo(bookInfo);
            if (v)
            {
                RefreshList();
            }
        }

        private void RefreshList()
        {
            IEnumerable<Model.BookInfo> enumerable = bookInfoUtils.SelectAll();
            uiDataGridView1.DataSource = enumerable;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            bool v = bookInfoUtils.EditBookInfo(bookInfo);
            if (v)
            {
                RefreshList();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click(object sender, EventArgs e)
        {
            bool v = bookInfoUtils.DeleteBookInfo(selId);
            if (v)
            {
                RefreshList();
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton4_Click(object sender, EventArgs e)
        {
            IEnumerable<Model.BookInfo> enumerable = bookInfoUtils.search(bookInfo);
            uiDataGridView1.DataSource = enumerable;
        }
        /// <summary>
        /// 选择一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int count = uiDataGridView1.SelectedRows.Count;
            if (count == 1)
            {
                DataGridViewRow row = uiDataGridView1.SelectedRows[0];
                selId = Convert.ToInt32(row.Cells[0].Value);
                bookInfo = bookInfoUtils.GetById(selId);
                if(bookInfo != null)
                {
                    uiTextBox1.Text = bookInfo.Name;
                    uiTextBox2.Text = bookInfo.Writer;
                    uiTextBox3.Text = bookInfo.Publisher;
                    uiComboBox1.Text = bookInfo.Type;
                    uiRichTextBox1.Text = bookInfo.Describe;
                }
                
            }
        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {
            bookInfo.Name = uiTextBox1.Text;
        }

        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {
            bookInfo.Writer = uiTextBox2.Text;
        }

        private void uiTextBox3_TextChanged(object sender, EventArgs e)
        {
            bookInfo.Publisher = uiTextBox3.Text;
        }

        private void uiComboBox1_TextChanged(object sender, EventArgs e)
        {
            bookInfo.Type = uiComboBox1.Text;
        }

        private void uiRichTextBox1_TextChanged(object sender, EventArgs e)
        {
            bookInfo.Describe = uiRichTextBox1.Text;
        }
    }
}
